package com.medical.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.medical.entity.TeacherOrder;

public interface TeacherOrderMapper extends BaseMapper<TeacherOrder> {
    int deleteByPrimaryKey(Integer id);

    int insertSelective(TeacherOrder record);

    TeacherOrder selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(TeacherOrder record);

    int updateByPrimaryKey(TeacherOrder record);
    
    @Select("(SELECT te.id as id,te.ordernumber as ordernum ,t.`name` as title,t.images as images,te.time as time,te.totalprice as price,te.states as states,1 as type FROM t_teacher_order as te LEFT JOIN t_teacher as t ON te.teacherid=t.id WHERE te.merchantid=#{merchantid} AND te.states in (${states})) UNION (SELECT io.id as id,io.ordernumber as ordernum ,i.`name` as title,i.images as images,io.time as time,io.totalprice as price,io.states as states,2 as type FROM t_interpreter_order as io LEFT JOIN t_interpreter as i ON io.interpreterid=i.id WHERE io.merchantid=#{merchantid} AND io.states in (${states})) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,3 as type FROM t_course_order as co LEFT JOIN t_course_streaming as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=2) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,4 as type FROM t_course_order as co LEFT JOIN t_course_offline as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=3) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,5 as type FROM t_course_order as co LEFT JOIN t_course_seeing as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=1) UNION (SELECT vo.id as id,vo.ordernumber as ordernum,v.`name` as title,v.images as images,vo.time as time,vo.totalprice as price,vo.paystates as states,6 as type FROM t_visa_order as vo LEFT JOIN t_visa as v ON vo.visaid=v.id  WHERE vo.merchantid=#{merchantid} AND vo.paystates in (${states})) UNION (SELECT	c.id AS id,c.ordernumber AS ordernum,cf.`name` AS title,cf.images AS images,cf.time AS time,c.totalprice AS price,c.states AS states,7 AS type FROM	t_conference_apply AS c	LEFT JOIN t_conference as cf on c.conferenceid=cf.id	WHERE	cf.userid =#{merchantid}	AND c.states IN (${states})) UNION (SELECT	c.id AS id,c.ordernumber AS ordernum,cn.`name` AS title,cn.images AS images,cn.time AS time,c.totalprice AS price,c.states AS states,8 AS type	FROM	t_convention_apply AS c	LEFT JOIN t_convention as cn	ON c.conventionid=cn.id	WHERE	cn.userid =#{merchantid}	AND c.states IN (${states})) ORDER BY	time")
    List<Map<String, Object>> getOrderList(@Param("merchantid")int merchantid,@Param("states")String states);
    
    @Select("(SELECT te.id as id,te.ordernumber as ordernum ,t.`name` as title,t.images as images,te.time as time,te.totalprice as price,te.states as states,1 as type FROM t_teacher_order as te LEFT JOIN t_teacher as t ON te.teacherid=t.id WHERE te.merchantid=#{merchantid} AND te.states in (${states})) UNION (SELECT io.id as id,io.ordernumber as ordernum ,i.`name` as title,i.images as images,io.time as time,io.totalprice as price,io.states as states,2 as type FROM t_interpreter_order as io LEFT JOIN t_interpreter as i ON io.interpreterid=i.id WHERE io.merchantid=#{merchantid} AND io.states in (${states})) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,3 as type FROM t_course_order as co LEFT JOIN t_course_streaming as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=2) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,4 as type FROM t_course_order as co LEFT JOIN t_course_offline as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=3) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.time as time,co.totalprice as price,co.states as states,5 as type FROM t_course_order as co LEFT JOIN t_course_seeing as cs ON co.courseid=cs.id WHERE co.merchantid=#{merchantid} AND co.states in (${states}) AND co.coursetype=1) UNION (SELECT vo.id as id,vo.ordernumber as ordernum,v.`name` as title,v.images as images,vo.time as time,vo.totalprice as price,vo.paystates as states,6 as type FROM t_visa_order as vo LEFT JOIN t_visa as v ON vo.visaid=v.id  WHERE vo.merchantid=#{merchantid} AND vo.paystates in (${states})) UNION (SELECT	c.id AS id,c.ordernumber AS ordernum,cf.`name` AS title,cf.images AS images,cf.time AS time,c.totalprice AS price,c.states AS states,7 AS type FROM	t_conference_apply AS c	LEFT JOIN t_conference as cf on c.conferenceid=cf.id	WHERE	cf.userid =#{merchantid}	AND c.states IN (${states})) UNION (SELECT	c.id AS id,c.ordernumber AS ordernum,cn.`name` AS title,cn.images AS images,cn.time AS time,c.totalprice AS price,c.states AS states,8 AS type	FROM	t_convention_apply AS c	LEFT JOIN t_convention as cn	ON c.conventionid=cn.id	WHERE	cn.userid =#{merchantid}	AND c.states IN (${states})) ORDER BY	time limit #{pageIndex},#{size}")
    List<Map<String, Object>> getOrderListPage(@Param("merchantid")int merchantid,@Param("states")String states,@Param("pageIndex")int pageIndex,@Param("size")int size);
    
    @Select("SELECT *,te.phone as telephone,te.address as orderAddress,te.time as orderTime,te.paytype as paytype,te.userid as yongId FROM t_teacher as t LEFT JOIN t_teacher_order as te ON te.teacherid=t.id WHERE te.id=#{id}")
    Map<String, Object> getOrderDateils(@Param("id")int id);
    
    @Select("	(SELECT te.id as id,te.ordernumber as ordernum ,t.`name` as title,t.images as images,te.states,te.time as time,te.totalprice as price,1 as type FROM t_teacher_order as te LEFT JOIN t_teacher as t ON te.teacherid=t.id WHERE te.userid=#{userid} AND te.states in (${states})) UNION (SELECT io.id as id,io.ordernumber as ordernum ,i.`name` as title,i.images as images,io.states,io.time as time,io.totalprice as price,2 as type FROM t_interpreter_order as io LEFT JOIN t_interpreter as i ON io.interpreterid=i.id WHERE io.userid=#{userid} AND io.states in (${states})) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,3 as type FROM t_course_order as co LEFT JOIN t_course_streaming as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=2) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,4 as type FROM t_course_order as co LEFT JOIN t_course_offline as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=3) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,5 as type FROM t_course_order as co LEFT JOIN t_course_seeing as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=1) UNION (SELECT vo.id as id,vo.ordernumber as ordernum,v.`name` as title,v.images as images,vo.paystates,vo.time as time,vo.totalprice as price,6 as type FROM t_visa_order as vo LEFT JOIN t_visa as v ON vo.visaid=v.id  WHERE vo.userid=#{userid} AND vo.paystates in (${states}) ) UNION 	(SELECT c.id AS id,c.ordernumber AS ordernum,cf.`name` AS title,cf.images AS images,c.states,c.time AS time,c.totalprice AS price,7 AS type	FROM	t_conference_apply AS c	LEFT JOIN t_conference as cf ON c.conferenceid=cf.id	WHERE	c.userid =#{userid}	AND c.states IN (${states})) UNION	(SELECT c.id AS id,c.ordernumber AS ordernum,cn.`name` AS title,cn.images AS images,c.states,c.time AS time,c.totalprice AS price,8 AS type	FROM	t_convention_apply AS c LEFT JOIN t_convention AS cn ON c.conventionid=cn.id	WHERE	c.userid = #{userid} AND c.states IN (${states})) ORDER BY	time")
    List<Map<String, Object>> findOrderListForUser(@Param("userid")int userid,@Param("states")String states);
    
    @Select("	(SELECT te.id as id,te.ordernumber as ordernum ,t.`name` as title,t.images as images,te.states,te.time as time,te.totalprice as price,1 as type FROM t_teacher_order as te LEFT JOIN t_teacher as t ON te.teacherid=t.id WHERE te.userid=#{userid} AND te.states in (${states})) UNION (SELECT io.id as id,io.ordernumber as ordernum ,i.`name` as title,i.images as images,io.states,io.time as time,io.totalprice as price,2 as type FROM t_interpreter_order as io LEFT JOIN t_interpreter as i ON io.interpreterid=i.id WHERE io.userid=#{userid} AND io.states in (${states})) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,3 as type FROM t_course_order as co LEFT JOIN t_course_streaming as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=2) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,4 as type FROM t_course_order as co LEFT JOIN t_course_offline as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=3) UNION (SELECT co.id as id,co.ordernumber as ordernum,cs.`name` as title,cs.images as images,co.states,co.time as time,co.totalprice as price,5 as type FROM t_course_order as co LEFT JOIN t_course_seeing as cs ON co.courseid=cs.id WHERE co.userid=#{userid} AND co.states in (${states}) AND co.coursetype=1) UNION (SELECT vo.id as id,vo.ordernumber as ordernum,v.`name` as title,v.images as images,vo.paystates,vo.time as time,vo.totalprice as price,6 as type FROM t_visa_order as vo LEFT JOIN t_visa as v ON vo.visaid=v.id  WHERE vo.userid=#{userid} AND vo.paystates in (${states}) ) UNION 	(SELECT c.id AS id,c.ordernumber AS ordernum,cf.`name` AS title,cf.images AS images,c.states,c.time AS time,c.totalprice AS price,7 AS type	FROM	t_conference_apply AS c	LEFT JOIN t_conference as cf ON c.conferenceid=cf.id	WHERE	c.userid =#{userid}	AND c.states IN (${states})) UNION	(SELECT c.id AS id,c.ordernumber AS ordernum,cn.`name` AS title,cn.images AS images,c.states,c.time AS time,c.totalprice AS price,8 AS type	FROM	t_convention_apply AS c LEFT JOIN t_convention AS cn ON c.conventionid=cn.id	WHERE	c.userid = #{userid} AND c.states IN (${states})) ORDER BY	time limit #{pageIndex},#{size}")
    List<Map<String, Object>> findOrderListForUserCount(@Param("userid")int userid,@Param("states")String states,@Param("pageIndex")int pageIndex,@Param("size")int size);
    
    @Select("SELECT COUNT(1) FROM t_teacher_order WHERE teacherid=#{teacherid}")
    int selectOrderCountByTeacherId(@Param("teacherid")int teacherid);
    
    @Update("UPDATE t_teacher_order SET states=#{states} WHERE id=#{id}")
    int updateStatesById(@Param("states")int states,@Param("id")int id);
}